package model

import (
	"errors"
	"gorm.io/gorm"
	"time"
)

type (
	StaffProjectModel interface {
		Insert(staff Staff) (err error)
		FindOneByTel(staff Staff) (Staff, error)
		FindOneByStaffId(staff Staff) (Staff, error)
		FindOneByProjectId(staff Staff) (Staff, error)
		Update(staff Staff) (err error)
		DeleteSoft(staff Staff) (err error)
		Delete(staff Staff) (err error)
	}
	DefaultStaffProjectModel struct {
		conn  *gorm.DB
		table string
	}

	Staff struct {
		Staff_id         int       `db:"staff_id"  json:"staff_id"`
		Staff_name       string    `db:"staff_name"  json:"staff_name"`
		Staff_Telephone  string    `db:"staff_telephone"  json:"staff_telephone"`
		Staff_Project_id int       `db:"staff_project_id"  json:"staff_project_id"`
		Create_time      time.Time `db:"create_time"  json:"create_time"`
		Delete_time      time.Time `db:"delete_time"  json:"delete_time"`
		DelState         int       `db:"del_state"  json:"del_state"`
		Password         string    `db:"password"  json:"password"`
		Role             string    `db:"role"  json:"role"`
	}
)

func NewDefaultStaffProjectModel(conn *gorm.DB) *DefaultStaffProjectModel {
	return &DefaultStaffProjectModel{
		conn:  conn,
		table: "staff",
	}
}

func (sta *DefaultStaffProjectModel) Insert(staff Staff) (err error) {
	staff.Create_time = time.Now()
	staff.Delete_time = time.Unix(0, 0)
	sta.conn.Create(&staff)
	return err
}

func (sta *DefaultStaffProjectModel) FindOneByStaffId(staff Staff) (Staff, error) {
	var err error
	var count int64
	sta.conn.Model(&staff).Where("staff_id=?", staff.Staff_id).Count(&count)
	if count <= 0 {
		err = errors.New("数据不存在")
		return Staff{}, err
	}
	sta.conn.Where("staff_id=?", staff.Staff_id).First(&staff)

	return staff, nil
}

func (sta *DefaultStaffProjectModel) FindOneByTel(staff Staff) (Staff, error) {
	var err error
	var count int64
	sta.conn.Model(&staff).Where("staff_telephone=?", staff.Staff_Telephone).Count(&count)
	if count <= 0 {
		err = errors.New("数据不存在")
		return Staff{}, err
	}
	sta.conn.Where("staff_telephone=?", staff.Staff_id).First(&staff)

	return staff, nil
}
func (sta *DefaultStaffProjectModel) FindOneByProjectId(staff Staff) (Staff, error) {
	var err error
	var count int64
	sta.conn.Model(&staff).Where("staff_project_id=?", staff.Staff_id).Count(&count)
	if count <= 0 {
		err = errors.New("数据不存在")
		return Staff{}, err
	}
	sta.conn.Where("staff_project_id=?", staff.Staff_id).First(&staff)

	return staff, nil
}
func (sta *DefaultStaffProjectModel) Update(staff Staff) (err error) {

	var count int64
	sta.conn.Model(&staff).Where("staff_id=?", staff.Staff_id).Count(&count)
	if count <= 0 {
		err = errors.New("数据不存在")
		return err
	}
	sta.conn.Model(&staff).Where("staff_id=?", staff.Staff_id).Updates(&staff)
	return nil
}

func (sta *DefaultStaffProjectModel) DeleteSoft(staff Staff) (err error) {
	staff.Delete_time = time.Now()
	staff.DelState = 1
	var count int64
	sta.conn.Model(&staff).Where("staff_id=?", staff.Staff_id).Count(&count)
	if count <= 0 {
		err = errors.New("数据不存在")
		return err
	}
	err = sta.Update(staff)
	if err != nil {
		return err
	}
	return nil
}

func (sta *DefaultStaffProjectModel) Delete(staff Staff) (err error) {
	var count int64
	sta.conn.Model(&staff).Where("staff_id=?", staff.Staff_id).Count(&count)
	if count <= 0 {
		err = errors.New("数据不存在")

		return err
	}
	sta.conn.Model(&staff).Where("staff_id=?", staff.Staff_id).Delete(&staff)
	return nil
}
